//***************************** 功能性函数 ********************************** */
// --------------日期格式化----------------------
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
// --------------判断当前时间是否在  两个日期节点内----------------------
function at_activity_time(stime, etime){
  let current_time = new Date().getTime()
  //console.log("当前时间：", new Date(), new Date().getTime())
  let start_time = new Date(stime).getTime()
  //console.log("开始时间：", new Date(stime), start_time)
  let end_time = new Date(etime).getTime()
  //console.log("结束时间：", new Date(etime), end_time)
  if (current_time < start_time || current_time > end_time) {
    return false;
  }
  return true;
}
//***************************** 获取用户信息  ********************************** */


//***************************** 客户端 读取数据库 ********************************** */
const db = wx.cloud.database()
// 1. 读取活动设置 和 卡券信息
function get_activity_settings(){
  return db.collection('activity_settings').doc('5cf7770f47882f40e4a3ceaa').get()
}
// 2. 获取当前用户openid、当前用户活动信息、 转发用户活动信息
function get_both_user_info(share_user_openid){
   return wx.cloud.callFunction({
    name:'getOpenid',
    data:{
      share_user_openid: share_user_openid
    }
  })
}
// 3. 向指定数据库添加记录
function cloud_add_record(collection_name, data){
  return db.collection(collection_name).add({
    // data 字段表示需新增的 JSON 数据
    data: data
  })
}
// 4. 添加点赞记录
function record_activity_log(give_user_openid, recv_user_openid){
  return wx.cloud.callFunction({
    name:'writeActivityLog',
    data:{
      give_user_openid: give_user_openid,
      recv_user_openid: recv_user_openid
    }
  })
}
// 5. 查询是否为相同用户点赞过
function already_give_like(give_user_openid, recv_user_openid){
  return db.collection('activity_log').where({
    give_openid: give_user_openid,
    recv_openid: recv_user_openid
  }).count()
}
// 6. 更新用户的 头像、昵称
function update_user_userinfo(current_user_openid,userInfo){
  return db.collection('activity_users').doc(current_user_openid).update({
    data: {
      user_avatar_url: userInfo.avatarUrl,
      user_nick_name: userInfo.nickName
    }
  })
}

/**
 * 兑换记录格式：
 * openid: xxxxx ,
 * ticketid:123, // 兑换卡券id
 * user_avatar_url:xxxx,
 * user_nick_name:xxx,
   consume_likes: 100 // 消耗多少赞
   remark:'兑换 ticket_name , 扣除 ticket.need_likes'
 */

// 6. 查询当前用户兑换某个卡券的数量
function already_exchange_count(current_user_openid, ticketid) {
  return db.collection('activity_exchange_record').where({
    openid: current_user_openid,
    ticketid: ticketid
  }).count()
}

// 7. 查询当前用户是否已经完成开卡操作
function whether_have_vip_card(openid){
  return db.collection('users').where({
    openid: openid // 填入当前用户 openid
  }).get()
}

// 8. 打开会员卡界面

function openVipCardByCode(template_cardid, code) {
  wx.showLoading({
    title: '正在打开会员卡',
  })
  wx.openCard({
    cardList: [{
      cardId: template_cardid,
      code: code
    }],
    success(res) {
      wx.hideLoading()
    }
  })
}

// 9. 获取排行榜数据
function get_rank_data(){
  return db.collection('activity_users')
    .field({
      user_avatar_url: true,
      user_nick_name: true,
      current_likes: true
    })
    .orderBy('current_likes', 'desc')
    .limit(5)
    .get()
}


function deleteTempOrderByOrderId(orderId) {
  return wx.cloud.callFunction({
    name: 'deleteTempOrder',
    data: {
      orderId: orderId
    }
  })
}

function run_when_page_shows(){
  // 
  get_activity_settings().then(res => {
    console.log(res.data)
    let settings = res.data.settings
    let in_time_span = task.at_activity_time(settings.start_time, settings.end_time)
    console.log("当前时间在 " + settings.start_time + " 和 " + settings.end_time + "之间吗：", in_time_span)
    console.log(task.formatTime(settings.start_time))
  })
}
module.exports = {
  formatTime: formatTime,
  deleteTempOrderByOrderId: deleteTempOrderByOrderId,
  get_activity_settings: get_activity_settings,
  at_activity_time: at_activity_time,
  get_both_user_info: get_both_user_info,
  cloud_add_record: cloud_add_record,
  record_activity_log: record_activity_log,
  already_give_like: already_give_like,
  already_exchange_count: already_exchange_count,
  update_user_userinfo: update_user_userinfo,
  whether_have_vip_card: whether_have_vip_card,
  openVipCardByCode: openVipCardByCode,
  get_rank_data: get_rank_data
}
